<?php

namespace App\Http\Controllers\Api\v1;

use App\Models\App;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller as BaseController;
use Symfony\Component\HttpKernel\Exception\HttpException;
use function PHPUnit\Framework\isEmpty;

class Controller extends BaseController
{
    const DEFAULT_LIMIT = 20;
    /**
     * 成功响应
     * @param $data
     * @param int $httpCode
     * @return \Illuminate\Http\JsonResponse|object
     */
    public function success($data = null, $httpCode = Response::HTTP_OK,$msg = null)
    {
        if ($msg == null){
            $msg = trans('error.' . 0);
        }
        return response()->json([
            'code' => 0,
            'now' => time(),
            'msg' => $msg,
            'data' => $data,
           // 'now' => Carbon::now()->timestamp
        ])->setStatusCode($httpCode);
    }

    public function fail($code, $msg = '')
    {
        return response()->json([
            'code' => $code,
            'now' => time(),
            //'msg' => $msg ?: getMsg($code),
            'msg' => $msg ?: trans('error.' . 300004),
            //'now' => Carbon::now()->timestamp
        ])->setStatusCode(Response::HTTP_OK);
    }

    protected function paging($page, $limit): int
    {
        $page = $page - 1;
        if ($page != 0) {
            $page = $limit * $page;
        }
        return $page;
    }

    public function errorResponse($statusCode, $message = null, $code = 0)
    {
        throw new HttpException($statusCode, $message, null, [], $code);
    }

    /**
     * 订单返回数据格式
     * @param null $data
     * @param int $total_count
     * @param int $last_page
     * @param int $httpCode
     * @return \Illuminate\Http\JsonResponse|object
     */
    public function orderSuccess($data = null, $total_count = 0, $last_page = 1, $httpCode = Response::HTTP_OK)
    {
        return response()->json([
            'code' => 0,
            'msg' => '',
            'data' => $data,
            'total_count' => $total_count,
            'last_page' => $last_page,
            'now' => Carbon::now()->timestamp,
        ])->setStatusCode($httpCode);
    }

    public function paginate($list)
    {
        $paginate['current_page'] = $list->currentPage();
        $paginate['last_page'] = $list->lastPage();
        $paginate['per_page'] = (int) $list->perPage();
        $paginate['total'] = $list->total();

        return $paginate;
    }

    //AES加密
    public function encrypt($data,$appseret){
        //加密
        //{"name":"test app name好","wapurl":"https:\/\/www.baidu.com","iswap":1,"splash":"https:\/\/swanshop.oss-ap-south-1.aliyuncs.com\/swanShop\/uploads\/goodsti\/2022081908\/5683f1374e0dc1b3cd00b89007ce46700f.jpg_600x600q80.jpg_.webp"}
        if($data->name == 'Pusoy Go'){
            $str = json_encode($data,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
        }else{
            $str = json_encode($data,JSON_UNESCAPED_UNICODE);
        }

        $aesjson = openssl_encrypt($str, 'AES-128-ECB', $appseret);
        return $aesjson;
    }

    public function checkShop($user)
    {
        if(!$user || $user->state != User::STATE_PASS){
            return $this->errorResponse(Response::HTTP_OK, trans('error.600001'), 600001);
        }
    }





}
